package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.dapan.DapanDimDictDefine;
import com.hllxd.hism.star.basic.service.service.DimDefineManagerService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/dim_define")
public class DimDefineManagerController2 {

    @Autowired
    private DimDefineManagerService defineManagerService;

    @Data
    public static class Param {
        public Long id;
        public String interval;
        public String typeCode;

        public String search;
    }

    @RestGrpcMapping("/listDimDefine.do")
    public NTSTATUS listDimDefine(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            result.setData(defineManagerService.listDimDefine(aa.search));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/searchDimDefine.do")
    public NTSTATUS searchDimDefine(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            result.setData(defineManagerService.searchDimDefine(aa.typeCode));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/getDimDefine.do")
    public NTSTATUS getDimDefine(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            DapanDimDictDefine item = defineManagerService.getDimDefine(aa.getId());
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setDimDefine.do")
    public NTSTATUS setDimDefine(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DapanDimDictDefine aa = params.driverObject(DapanDimDictDefine.class);
            Object res = defineManagerService.setDimDefine(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/delDimDefine.do")
    public NTSTATUS delDimDefine(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Object res = defineManagerService.delDimDefine(aa.getId());
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
